<?php

function set_reporting() {
	if (VERBOSE_ERROR == true) {
		error_reporting(E_ALL);
		ini_set('display_errors','On');
	} else {
		error_reporting(E_ALL);
		ini_set('display_errors','Off');
		ini_set('log_errors', 'On');
		ini_set('error_log', ROOT . DS . 'tmp' . DS . 'logs' . DS . 'error.log');
	}
}

function stripslashes_deep($value) {
	$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
	return $value;
}

function remove_magic_quotes() {
	if (get_magic_quotes_gpc()) {
		$_GET = stripslashes_deep($_GET);
		$_POST = stripslashes_deep($_POST);
		$_COOKIE = stripslashes_deep($_COOKIE);
	}
}

function unregister_globals() {
	if (ini_get('register_globals')) {
		$array = array('_SESSION', '_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
		
		foreach ($array as $value) {
			foreach ($GLOBALS[$value] as $key => $var) {
				if ($var === $GLOBALS[$key]) {
					unset($GLOBALS[$key]);
				}
			}
		}
	}
}

function route_url($url) {
	global $routing;

	foreach ($routing as $pattern => $result) {
		if (preg_match($pattern, $url)) {
			return preg_replace($pattern, $result, $url);
		}
	}

	return ($url);
}

function dispatch() {
	global $url;
	global $default;

	$query_string = array();

	if (!isset($url)) {
		$controller = $default['controller'];
		$action = $default['action'];
	} else {
		$url = route_url($url);
		$url_array = array();
		$url_array = explode("/", $url);
		$controller = $url_array[0];
		array_shift($url_array);
		
		if (isset($url_array[0])) {
			$action = $url_array[0];
			array_shift($url_array);
		} else {
			$action = 'index';
		}
		
		$query_string = $url_array;
	}
	
	$controller_name = ucfirst($controller);
	$dispatch = new $controller_name($controller, $action);
	
	if ((int)method_exists($controller_name, $action)) {
		if ((int)method_exists($controller_name, "beforeAction")) {
			call_user_func_array(array($dispatch, "beforeAction"), $query_string);
		}
		
		call_user_func_array(array($dispatch, $action), $query_string);
		
		if ((int)method_exists($controller_name, "afterAction")) {
			call_user_func_array(array($dispatch, "afterAction"), $query_string);
		}
	} else {
		/* Method tidak ditemukan */
	}
}

function __autoload($class_name) {
	//echo "Autoloading $class_name<br />";
	if (file_exists(ROOT . DS . 'library' . DS . strtolower($class_name) . '.class.php')) {
		require_once(ROOT . DS . 'library' . DS . strtolower($class_name) . '.class.php');
	} else if (file_exists(ROOT . DS . 'application' . DS . 'controllers' . DS . strtolower($class_name) . '.php')) {
		require_once(ROOT . DS . 'application' . DS . 'controllers' . DS . strtolower($class_name) . '.php');
	} else if (file_exists(ROOT . DS . 'application' . DS . 'models' . DS . strtolower($class_name) . '.php')) {
		require_once(ROOT . DS . 'application' . DS . 'models' . DS . strtolower($class_name) . '.php');
	} else {
		/* Kelas tidak ditemukan */
	}
}

$session = new Session();
$imgedit = new ImageEdit();

set_reporting();
remove_magic_quotes();
unregister_globals();
dispatch();